﻿using Biomedica.Graphics;
using Biomedica.Screen.Mappers;
using DTWGestureRecognition;
using KinectLibrary;
using KinectWindowLib;
using KinectWindows.Math;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NuiDevices.Nui
{
    // Start the Nui Engine to Grabb the feilds
    public class KinectHandGrabber
    {

      
        public Vector2 LefthandLocation { set; get; }
        public Vector2 RighthandLocation { set; get; }
        KinectGestureManager eng;
        public DtwGestureRecognizer GestureRecogniser;
        private ScreencordMapper Screen_mapper;
        public KinectHandGrabber()
        {
            Screen_mapper = new ScreencordMapper(Display.ScreenCanvas);
        }
        internal KinectGestureManager GetNuiEngine()
        {

            return eng;
        }
        public KinectGestureManager _Manager { get; set; }
        internal void StartSensor()
        {
            _Manager = KinectGestureManager.New();
            _Manager.EnableKinect = true;
            _Manager.OnJointFrameReady += JointFrameReady;

        }
        internal void JointFrameReady(Vector3 Lefthand, Vector3 RightHand, Vector3 ShoulderLeft, Vector3 ShoulderRight, Vector3 Spine, Vector3 Head, Vector3 Position)
        {
            Update(Lefthand, RightHand, ShoulderLeft, ShoulderRight, Spine, Head, Position);
        }

        public void Update(Vector3 Lefthand, Vector3 RightHand, Vector3 ShoulderLeft, Vector3 ShoulderRight, Vector3 Spine, Vector3 Head, Vector3 Position)
        {
            //{

            #region mouse tracking

            Vector2 v1 = Vector2.Zero;
            Vector2 v2 = Vector2.Zero;
            Screen_mapper = new ScreencordMapper(screen.ScreenCanvas);
            Vector3 cog_postion = Vector3.Zero;
            Screen_mapper.SmothingFactor = 1.2;

            //  if (!IsLeftHanded) cog_postion = left_shoulder;
            //  else cog_postion = right_shoulder;
            Vector3 center_shoulder = ShoulderRight - ShoulderLeft;
            cog_postion = center_shoulder;

            Vector2[] Vectors = new Vector2[7];
            #region Mapcord to screen Cord
            Vectors[0] = Screen_mapper.MapKinecktToScreen(cog_postion, Lefthand);
            Vectors[1] = Screen_mapper.MapKinecktToScreen(cog_postion, RightHand);
            Vectors[2] = Screen_mapper.MapKinecktToScreen(cog_postion, ShoulderLeft);
            Vectors[3] = Screen_mapper.MapKinecktToScreen(cog_postion, ShoulderRight);
            Vectors[4] = Screen_mapper.MapKinecktToScreen(cog_postion, Spine);
            Vectors[5] = Screen_mapper.MapKinecktToScreen(cog_postion, Head);
            Vectors[6] = Screen_mapper.MapKinecktToScreen(cog_postion, Position);
            #endregion
            #region update hand postions on screen
            v2 = eng._filterL.FilterJointPostion(Microsoft.Kinect.KinectSensor.KinectSensors[App.ActiveSensor], Vectors[0]);
            v1 = eng._filterR.FilterJointPostion(Microsoft.Kinect.KinectSensor.KinectSensors[App.ActiveSensor], Vectors[1]);


            //  NuiEventManager.PostEvent(new NuiEvent(KinectWindows.Messages.WindowMessages.NUI_MSG_LEFT_HAND_MOVED, new Object[] { v2 }));
            // NuiEventManager.PostEvent(new NuiEvent(KinectWindows.Messages.WindowMessages.NUI_MSG_RIGHT_HAND_MOVED, new Object[] { v1 }));
            NuiEventManager.PostEvent(new NuiEvent(KinectWindows.Messages.WindowMessages.NUI_MSG_SKELETON_POS_CHANGED, Vectors));
            #endregion
            #endregion

            #region Detection Cycles and filters

            //App.Nav.StartPostureDetectionCycle(head, spine, left_hand, right_hand, right_shoulder, left_hand, center_shoulder, skelpos);

            #endregion
        }

    }
    // Start the Gestur Engine
    public class KinectGestureGrabber
    {
        public String GestureName;
        private KinectGestureManager KinecktGrabber { set; get; }
        DtwGestureRecognizer GestureRecogniser;
        public KinectGestureGrabber()
        {
            //12, 0.6, 2, 2, 10
            dim = 12;
            threshold = 0.6;
            firstThreshold = 2;
            minLen = 10;


        }
        public static KinectGestureGrabber CreateKinectGestureGrabber(KinectHandGrabber eng)
        {
            KinectGestureGrabber g = new KinectGestureGrabber();
            g.KinecktGrabber = eng._Manager;
            g.KinecktGrabber.OnJointFrameReady += g.JointFrameReady;

            g.GestureRecogniser = eng.GestureRecogniser;
            GestureTools.LoadGesturesFromString(KinectWindows.Properties.Resources._2011_07_04_22_09, ref GestureRecogniser);
            eng._Manager._capturing = true;
            return g;
        }

        protected void JointFrameReady(Vector3 Lefthand, Vector3 RightHand, Vector3 ShoulderLeft, Vector3 ShoulderRight, Vector3 Spine, Vector3 Head, Vector3 Position)
        {

        }





        public double minLen { get; set; }

        public double firstThreshold { get; set; }

        public double threshold { get; set; }

        public int dim { get; set; }
    }
    // Start The Speech Grabber
    public class SpeechGraber
    {
        String Command { set; get; }
        public SpeechCommander commanders { get; set; }
        public SpeechGraber()
        {
            commanders = new SpeechCommander();
            commanders.OnSpeechRecognised += commanders_OnSpeechRecognised;
            commanders.Run(Microsoft.Kinect.KinectSensor.KinectSensors[App.ActiveSensor]);
        }
        static void commanders_OnSpeechRecognised(object sender, SpeechRecogised e)
        {

        }
    }
        
       
}
